import json
from jsonpath import jsonpath


# 取列表中第几个或者倒数第几个,默认第一个
def get_json_path(param, x=1):
    if isinstance(param, list):
        param = param[x - 1] if x > 0 else param[x]
        print(param)
        return param
    if isinstance(param, str):
        param = json.loads(param)
        return param


# json
def get_variable(data, num=0):
    if isinstance(data, str):
        variable = json.loads(data)
    base_variable = data(num)
    return base_variable


# form_data
def form_data(v_list, num=0):
    if isinstance(v_list, str):
        a = v_list.split('&')[num]
        base_a = a.split('=')[1]
    return base_a


# list->str
def transformation_str(v_list):
    if v_list is None:
        return ""
    if isinstance(v_list, list):
        base_str = json.dumps(v_list)
        return base_str